#include"Protocol.hpp"
#include"sock.hpp"

void  Usage(string str)
{
  cout<<"Usage: "<<str<<" id"<<" port"<<endl;
}
int main(int argc,char* argv[])
{
  if(argc!=3)
  {
    Usage(argv[0]);
    exit(-1);
  }

  int sock = Sock::Socket();
  Sock::Connect(sock,argv[1],atoi(argv[2]));

  //业务逻辑

  request_t req;
  while(1){
    memset(&req,0,sizeof(req));
    cout<<"Please Enter Data one#: ";
    cin>>req.x;
    cout<<"Please Enter Data two#: ";
    cin>>req.y;
    cout<<"Please Enter operator#: ";
    cin>>req.op;
    string json_string =  SerializeRequest(req);//序列化
    write(sock,json_string.c_str(),json_string.size());

    char buffer[1024]={0};
    ssize_t s =   read(sock,buffer,sizeof(buffer)-1);
    if(s>0)
    {      
      response_t resp;
      string str = buffer;
      DeserializeRespon(str,resp);//反序列化

      cout<<"code: "<<resp.code<<endl;
      cout<<"result: "<<resp.result<<endl;

    }

  }

  return 0;
}
